require(['select2', 'date', 'validator', 'grid'], function () {
    var $qImportDate = $("#qImportDate"),$qErrorType = $("#qErrorType"),$errInfoSearchBnt = $("#errInfoSearchBnt"),
        $formData = $("#formData"),$licPltNo = $("#licPltNo"),$drvExamUploadErrForm=$("drvExamUploadErrForm");
    $formData.initForm();
    var corpId;

    examErrInfo = function (_corpId) {
    	corpId=_corpId;
        $qImportDate.val(new Date().format('yyyy-MM-dd'));
        if($qImportDate){
            //获取错误信息
            $.ajaxExt({
                url : "/baseDriverExamInfo/getDriverExamUploadErrList.htm",
                data : {
                    'licPltNo' : $licPltNo.val(),
                    'qImportDate' : $qImportDate.val(),
                    'qErrorType' : $qErrorType.val(),
                    'corpId' : corpId
                }
            }).done(function (data) {
                if(data){
                    showErrInfoList(data); // 第一次打开，有数据就显示
                }else{
                    $(".err_info_tr").remove(); // 没数据，就清空旧数据
                }
            });
        }

    };

    // 错误信息过滤
    $errInfoSearchBnt.on("click", function(){
        $.ajaxExt({
            url : "/baseDriverExamInfo/getDriverExamUploadErrList.htm",
            data : {
                'licPltNo' : $licPltNo.val(),
                'qImportDate' : $qImportDate.val(),
                'qErrorType' : $qErrorType.val(),
                'corpId' : corpId
            }
        }).done(function (data) {
            showErrInfoList(data);
        });
    });
    // 展示错误信息列表
    showErrInfoList = function(dataList){
        var cnt = 0;
        $(".err_info_tr").remove();
        if(dataList){
            $.each(dataList,function(n,entity){
                var oprtLine = entity.oprtLine,
                    licPltNo = entity.licPltNo,
                    drvName = entity.drvName,
                    timecardCardno = entity.timecardCardno,
                    signInTime = entity.signInTime,
                    signOutTime = entity.signOutTime,
                    importTime = entity.importTime;
                // 数据格式化
                if(!oprtLine){
                    oprtLine = "";
                }
                if(!licPltNo){
                    licPltNo = "";
                }
                if(!drvName){
                    drvName = "";
                }
                if(!timecardCardno){
                    timecardCardno = "";
                }
                if(!signInTime){
                    signInTime = "";
                }
                if(!signOutTime){
                    signOutTime = "";
                }
                importTime = importTime.substring(importTime.indexOf(" "));
                // 显示数据
                var content = "<tr class='err_info_tr'>"
                    +"<input type='hidden' name='uptErrorIds' value='"+entity.errorId+"'>"
                    +"<input type='hidden' name='corpIds' value='"+entity.corpId+"'>"
                    +"<td><input class='err-c-item' delErrorId='"+entity.errorId+"' type='checkbox'></td>"
                    +"<td><input type='text' name='uptOprtLines' value='"+oprtLine+"' data-validator='{required:true}' class='input-text' style='width:50px' ></td>"
                    +"<td><input type='text' name='uptLicPltNos' value='"+licPltNo+"' data-validator='{required:true}' class='input-text' style='width:60px'  ></td>"
                    +"<td><input type='text' name='uptDrvNames' value='"+drvName+"' data-validator='{required:true}' class='input-text' style='width:50px'  ></td>"
                    +"<td><input type='text' name='uptTimecardCardnos' value='"+timecardCardno+"' data-validator='{required:true}' class='input-text'  style='width:70px'  ></td>"
                    +"<td><input type='text' id='upt_beginTime"+cnt+"' name='uptSignInTimes' value='"+signInTime+"' data-validator='{required:true}' class='input-text'  style='width:135px'  onclick=\"WdatePicker({isShowClear:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})\"></td>"
                    +"<td><input type='text' id='upt_endTime"+cnt+"' name='uptSignOutTimes' value='"+signOutTime+"' data-validator=\"{dateCompare:'upt_beginTime"+cnt+"',required:true}\"  style='width:135px'  class='input-text'  onclick=\"WdatePicker({isShowClear:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})\"></td>"
                    +"<td>"+entity.errorType+"</td>"
                    +"<td>"+entity.errorHint+"</td>"
                    +"<td>"+importTime+"</td>"
                    +"<td class='u-btn2'>"
                    +"<div class='btnbox btnbox2'>"
                    +"<a class='operate operate-delete' href='javascript:;' onclick=delErrInfo('"+entity.errorId+"',this);>删除 </a>"
                    +"<div class='bor'></div>"
                    +"</div>"
                    +"</td>"
                    +"</tr>";
                $("#drvExamUploadErrGrid tbody").append(content);
                cnt++;
            });
        }else{
            $.messager.alert("提示","暂无数据！","info");
        }
    };

    // 删除错误信息
    delErrInfo = function(errorId,a){
        var that = a;
        $.messager.confirm("是否删除","是否删除该考勤上传错误信息？",function(r){
            if(r){
                $.ajaxExt({
                    url : "/baseDriverExamInfo/delErrInfo.htm",
                    data : {'errorId': errorId}
                }).done(function(){
                    $(that).closest('tr').remove();
                });
            }
        });
    }

    loadAgagin = function() {
        var result = $.validator($drvExamUploadErrForm);
        if (result) {
            $.messager.confirm("是否导入", "是否保存驾驶员考勤信息？", function (r) {
                if (r) {
//                	console.info($("#drvExamUploadErrForm").serialize());
//                	console.info(corpId);
                    // 将驾驶员考勤信息传到后台
                    $.ajaxExt({
                        url: "/baseDriverExamInfo/reuploadDriverExam.htm",
                        data: $("#drvExamUploadErrForm").serialize()
                    }).done(function (data) {
                        if (data) {
                            $.ajaxExt({
                                url: "/baseDriverExamInfo/getDriverExamUploadErrList.htm",
                                data: {
                                    'licPltNo' : $licPltNo.val(),
                                    'qImportDate': $qImportDate.val(),
                                    'qErrorType': $qErrorType.val(),
                                    'corpId' : corpId
                                }
                            }).done(function (data) {
                                // 错误信息data。文件数据部分导入，请在“错误信息查看”中编辑异常数据，重新进行导入。
                                showErrInfoList(data);
                                $.messager.alert("提示", "文件数据部分导入，请进行编辑，重新导入！", "info");
                            });
                        } else {
                            $.messager.alert("提示", "文件数据已全部导入！", "info");
                        }
                    });
                }
            });
        }
    }
});